#include "flag_lab_le_lab.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QDebug>

/**********************************************
* @file       flag_lab_le_lab.cpp
* @brief
* "tcp_Pisiton": {
        "flag_lab_le_lab": {
          "tcp_Px": {
            "label": "Px(mm)",
            "lineedit": "Px"
          },
          "tcp_Py": {
            "label": "Py(mm)",
            "lineedit": "Py"
          },
          "tcp_Pz": {
            "label": "Pz(mm)",
            "lineedit": "Pz"
          }
        }
      },
* @author     yfk
* @date       2025-07-24
**********************************************/


Flag_Lab_Le_Lab::Flag_Lab_Le_Lab(const QJsonObject &flagDetails, QWidget *parent)
    : QWidget(parent)
{
    setupControlLayout(flagDetails);
}

void Flag_Lab_Le_Lab::setupControlLayout(const QJsonObject &flagDetails)
{
    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->setContentsMargins(6, 6, 6, 6);
    mainLayout->setSpacing(12);

    for (const QString &key : flagDetails.keys()) {
        QJsonObject item = flagDetails[key].toObject();
        QString labelText = item["label"].toString();
        QString lineEditName = item["lineedit"].toString();

        QLabel *label = new QLabel(labelText, this);
        label->setStyleSheet("color: #ccc; font-size: 13px; font-weight: normal;");

        QLineEdit *lineEdit = new QLineEdit(this);
        lineEdit->setObjectName(lineEditName);
        lineEdit->setFixedHeight(36);
        lineEdit->setMinimumWidth(160);
        lineEdit->setStyleSheet(R"(
            QLineEdit {
                background-color: #2e3a50;
                border: 1px solid #3c445c;
                border-radius: 6px;
                padding: 6px 12px;
                color: white;
                font-weight: bold;
            }
        )");

        QVBoxLayout *fieldLayout = new QVBoxLayout();
        fieldLayout->setSpacing(4);
        fieldLayout->addWidget(label);
        fieldLayout->addWidget(lineEdit);

        QWidget *fieldWidget = new QWidget(this);
        fieldWidget->setLayout(fieldLayout);

        mainLayout->addWidget(fieldWidget);

        lineEdits.insert(lineEditName, lineEdit);

        connect(lineEdit, &QLineEdit::textChanged, this, [=](const QString &val) {
            emit valueChanged(lineEditName, val);
        });
    }

    setLayout(mainLayout);
}
